package com.atguigu.spzx.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.h5.UserInfoVo;
import com.atguigu.spzx.user.mapper.UserInfoMapper;
import com.atguigu.spzx.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author Mahone
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void register(UserRegisterDto userRegisterDto) {
        System.out.println("用户注册");

        // 判断用户名是否被占用
        String username = userRegisterDto.getUsername();
        Integer count = userInfoMapper.selectCountByUsername(username);
        Assert.isTrue(count <= 0,"该用户已注册");

        // 手机短信验证码
        String code = userRegisterDto.getCode();
        String codeCache =  redisTemplate.opsForValue().get("phone:code:" + code);
        Assert.isTrue(code.equals(codeCache),"验证码错误");

        // 注册用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(DigestUtils.md5DigestAsHex(userRegisterDto.getPassword().getBytes()));
        userInfo.setNickName(userRegisterDto.getNickName());
        userInfo.setPhone(userRegisterDto.getUsername());

        userInfoMapper.insertUserInfo(userInfo);
    }

    @Override
    public Object login(UserLoginDto userLoginDto, String ipAddress) {
        String username = userLoginDto.getUsername();
        // 查找用户
        UserInfo userInfo = userInfoMapper.selectByUsername(username);
        Assert.notNull(userInfo,"用户名不正确");
        // 密码校验
        String password = userLoginDto.getPassword();
        String userPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        String dbPassword = userInfo.getPassword();
        Assert.isTrue(dbPassword.equals(userPassword),"密码错误");
        // 生成token
        String token = UUID.randomUUID().toString().replace("-","");
        // 存入redis
        redisTemplate.opsForValue().set("user:login:" + token, JSON.toJSONString(userInfo),1, TimeUnit.HOURS);
        // 修改用户登录时间 ip
        userInfo.setLastLoginIp(ipAddress);
        userInfo.setLastLoginTime(new Date());
        // 更新用户信息
        userInfoMapper.updateById(userInfo);
        return token;
    }

    @Override
    public UserInfoVo getCurrentUserInfo(String token) {
        // 从redis取
        String userInfoJson = redisTemplate.opsForValue().get("user:login:" + token);
        Assert.notNull(userInfoJson,"用户未登录");

        UserInfo userInfo = JSON.parseObject(userInfoJson,UserInfo.class);
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(userInfo.getNickName());
        userInfoVo.setAvatar(userInfo.getAvatar());
        return userInfoVo;
    }

    @Override
    public UserInfo getCurrentUserInfoByToken(String token) {
        // 从redis取
        String userInfoJson = redisTemplate.opsForValue().get("user:login:" + token);
        Assert.notNull(userInfoJson,"用户未登录");
        UserInfo userInfo = JSON.parseObject(userInfoJson,UserInfo.class);
        return userInfo;
    }


}
